<template>
  <div class="home">
    <div>
      <input
       class="input"
       type="text"
       v-model="inputvalue"
       placeholder="请输入任务名称"
       @keyup.enter = "addTask"
       />
    </div>
    <ul class="ul">
      <li class="item" v-for="(item,index) in taskList" :key="index">
       <p 
       class="content"
       :class="item.isFinished? 'active' : ''"
       @click="updateTask(index,!item.isFinished)"
       >{{item.label}}</p>
       <div class="item-delete"
       @click="deleteTask(index)">X</div>
      </li>
      <li v-if="taskList.length === 0 " class="item-none">暂时无数据</li>
    </ul>
  </div>
</template>

<script lang="ts">
 
 import{ref,computed} from 'vue'
 import { useStore} from 'vuex'
 export default{
   setup(){

     const store = useStore()
     const inputvalue = ref('')
     const taskList = computed(() => store.state.taskList);
     const addTask = () => {
       store.commit('createTask',{
         label: inputvalue.value,
         isfinished: false
       })
       inputvalue.value = ''

     }
     const updateTask = (index, status) => {
       store.commit('updateTask',{
         index,
         status
       })
     }
     const deleteTask = (index) =>{
       store.commit("deleteTask",index)

     }

     return{

       taskList,
       inputvalue,
       addTask,
       updateTask,
       deleteTask
     }
   }
 }
</script>
<style scoped lang='scss'>
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
ul,
li {
  list-style: none;

  text-align: left;
}
.home {
  max-width: 400px;
  margin: 0 auto;
  .input {
    width: 100%;
    height: 40px;
    border-radius: 5px;
    outline-style: none;
    border: 2px solid #999;
    padding: 5px 10px;
  }
  .ul {
    margin-top: 10px;
  }

  .item {
    height: 40px;
    line-height: 40px;
    padding-bottom: 5px;
    border-bottom: 1px solid #dcdfe6;
    color: #333333;
  }
  .item-none {
    height: 40px;
    line-height: 40px;
    padding-bottom: 5px;
    color: #333333;
    text-align: center;
  }
  .content {
    float: left;
    height: 40px;
    line-height: 40px;
    cursor: pointer;
  }
  p.active {
    text-decoration:line-through; 
    color: #999999;
  }
  .item-delete {
    float: right;
    width: 25px;
    text-align: center;
    cursor: pointer;
  }
}
</style>
